package com.wantong.jianpu.viewmodels;

import android.app.Application;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.util.SmartGlideImageLoader;
import com.wantong.jianpu.R;
import com.wantong.jianpu.activitys.AssignActivity;
import com.wantong.jianpu.activitys.FaultCreateActivity;
import com.wantong.jianpu.activitys.FaultHandleActivity;
import com.wantong.jianpu.activitys.FaultProgressActivity;
import com.wantong.jianpu.activitys.FaultSuccessActivity;
import com.wantong.jianpu.adapters.DetailImageAdapter;
import com.wantong.jianpu.base.BaseResultBean;
import com.wantong.jianpu.beans.DeviceBean;
import com.wantong.jianpu.beans.FaultDetailBean;
import com.wantong.jianpu.event.FaultDetailEvent;
import com.wantong.jianpu.models.FaultModel;
import com.wantong.jianpu.net.BaseObserver;
import com.wantong.jianpu.utils.Constants;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * @author PACY
 * @date 2022/4/25
 * description:  管理员发现故障并且上报     只会无反应或者显示确认故障修复
 *                站长提交故障到机电部门或者退回
 *                机电管理员分配维修人员或者退回
 *                维修人员维修或者不修  维修完成回到上报人员确认
 *
 *
 *
 */
public class FaultDetailViewModel extends BaseViewModel<FaultModel> {

    public String id;
    public FaultModel model;

    //9,不显示
    //0确认上报 对应UI 处理
//    1，工单指派、工单退回
//    2，抢单
//    3，上传维修进度、纠错报错信息
//    4，工单转派、工单退回
//    5，确认故障修复
//    6，工单确认
    public SingleLiveEvent<Integer> status=new SingleLiveEvent<>();
    public DetailImageAdapter mAdapter;
    public SingleLiveEvent<Boolean> isOpen=new SingleLiveEvent<>();//收起/展开
    public SingleLiveEvent<Boolean> reBackDialogEvent=new SingleLiveEvent<>();//工单退回


    public SingleLiveEvent<FaultDetailBean> detail=new SingleLiveEvent<>();//详细信息
    public SingleLiveEvent<List<FaultDetailBean.RecordsListDTO>> records=new SingleLiveEvent<>();//流程数据




    public FaultDetailViewModel(@NonNull Application application) {
        super(application);
        model=new FaultModel();
    }

    @Override
    protected void init() {
        super.init();
        initAdapter();
        isOpen.postValue(false);
        getDetailRequest();
        getDetailStatus();
    }

    /**
     * 刷新
     */
    public void refresh(){
        getDetailRequest();
        getDetailStatus();
    }

    private void initAdapter(){
        mAdapter=new DetailImageAdapter();
    }

    public BindingCommand openDetail=new BindingCommand(() -> {
        if(isOpen.getValue()){
            isOpen.postValue(false);
        }else{
            isOpen.postValue(true);
        }
    });


    /**
     * 查询报修详情
     */
    public void getDetailRequest(){
        showDialog();
        model.getRepairDetail(id, new BaseObserver<FaultDetailBean>() {
            @Override
            public void onSuccess(BaseResultBean<FaultDetailBean> t) {
                detail.setValue(t.getData());
                mAdapter.setNewInstance(t.getData().getFaultFiles());
                records.setValue(t.getData().getRecordsList());
            }

            @Override
            public void onComplete() {
                super.onComplete();
                dismissDialog();
            }
        },getLifecycleProvider());
    }

    /**
     * 查询报修详情状态
     */
    public void getDetailStatus(){
        showDialog();
        model.getRepairDetailStatus(id, new BaseObserver<Integer>() {
            @Override
            public void onSuccess(BaseResultBean<Integer> t) {
                status.setValue(t.getData());
            }

            @Override
            public void onComplete() {
                super.onComplete();
                dismissDialog();
            }
        },getLifecycleProvider());
    }


    /**
     * 工单退回
     */
    public void gdth(String remark){
        Map<String,Object> map=new HashMap<>();
        map.put("repairOrderId",id);
        map.put("remark",remark);
        showDialog();
        model.gdth(map, new BaseObserver() {
            @Override
            public void onSuccess(BaseResultBean t) {
                ToastUtils.show("退回成功");
                refresh();
            }

            @Override
            public void onComplete() {
                super.onComplete();
                dismissDialog();
            }
        },getLifecycleProvider());
    }


    /**
     * 处理
     */
    public BindingCommand toHandlePage=new BindingCommand(() -> {
        Bundle bundle=new Bundle();
        bundle.putString("id",id);
       startActivity(FaultHandleActivity.class,bundle);
    });

    /**
     * 指派维修人员
     */
    public BindingCommand toAssignPage=new BindingCommand(() -> {
        Bundle bundle=new Bundle();
        bundle.putString("id",id);
        startActivity(AssignActivity.class,bundle);
    });

    /**
     * 工单退回
     */
    public BindingCommand reBack=new BindingCommand(() -> {
        reBackDialogEvent.setValue(true);
    });

    /**
     * 纠错报修信息
     */
    public BindingCommand toCreatePage=new BindingCommand(() -> {
        Bundle bundle=new Bundle();
        bundle.putBoolean("isCreate",false);
        bundle.putString("id",id);
        startActivity(FaultCreateActivity.class,bundle);
    });

    /**
     * 上传维修进度
     */
    public BindingCommand toProgressPage=new BindingCommand(() -> {
        Bundle bundle=new Bundle();
        bundle.putString("id",id);
        startActivity(FaultProgressActivity.class,bundle);
    });

    /**
     * 确认故障详情
     */
    public BindingCommand toSuccessPage=new BindingCommand(() -> {
        Bundle bundle=new Bundle();
        bundle.putString("id",id);
        startActivity(FaultSuccessActivity.class,bundle);
    });





}
